//
//  WDTabBar.m
//  winterTest
//
//  Created by wd on 15/10/26.
//  Copyright © 2015年 wd ,Ltd. All rights reserved.
//

#import "WDTabBar.h"

@interface WDTabBar ()

/** 加号 按钮 */
@property (nonatomic, strong) UIButton<WDPlusButtonSubclassing> *plusButton;

@end

@implementation WDTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [self sharedInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self = [self sharedInit];
    }
    return self;
}

- (instancetype)sharedInit
{
    if (WDExternPushlishButton) {
        self.plusButton = WDExternPushlishButton;
        [self addSubview:(UIButton *)self.plusButton];
    }
    self.backgroundColor = [UIColor whiteColor];
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    if (WDExternPushlishButton) {
        CGFloat barWidth = self.bounds.size.width;
        CGFloat barHeight = self.bounds.size.height;
        
        // 计算item的宽度
        CGFloat tabBarButtonW = barWidth / (WDTabBarItemsCount + 1);
        NSInteger buttonIndex = 0;
        CGFloat mutiplerInCenterY;
        
        if ([[self.plusButton class] instancesRespondToSelector:@selector(multiplerInCenterY)]) {
            mutiplerInCenterY = [[self.plusButton class] multiplerInCenterY];
        } else {
            CGSize sizeOfPlusButton = self.plusButton.bounds.size;
            CGFloat heightDifference = sizeOfPlusButton.height - self.bounds.size.height;
            
            if (heightDifference < 0) {
                mutiplerInCenterY = 0.5;
            } else {
                CGPoint center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
                center.y = center.y - heightDifference/2.0;
                mutiplerInCenterY = center.y / self.bounds.size.height;
            }
        }
        self.plusButton.size = CGSizeMake(tabBarButtonW, barHeight);
        self.plusButton.center = CGPointMake(barWidth * 0.5, barHeight * mutiplerInCenterY);

        NSUInteger plusButtonIndex = 0;
        if ([[self.plusButton class] respondsToSelector:@selector(indexOfPlusButtonInTabBar)]) {
            plusButtonIndex = [[self.plusButton class] indexOfPlusButtonInTabBar];
            // 修改self.plusButton的x
            self.plusButton.frame = CGRectMake(plusButtonIndex*tabBarButtonW,
                                               CGRectGetMinY(self.plusButton.frame),
                                               CGRectGetWidth(self.plusButton.frame),
                                               CGRectGetHeight(self.plusButton.frame));
        } else {
            if (WDTabBarItemsCount % 2 != 0) {
                [NSException raise:@"WDTabBarController" format:@"如果WDTabbarControllers的个数是奇数，你必须在你自定义的plusButton中实现`+indexOfPlusButtonInTabBar`，来指定plusButton的位置"];
            } else {
                plusButtonIndex = WDTabBarItemsCount / 2;
            }
        }
        for (UIView *childView in self.subviews) {
            // 调整UITabBarButton的位置
            if ([childView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                if (buttonIndex == plusButtonIndex) {
                    buttonIndex++;
                }
                // 仅修改childView的宽度
                childView.frame = CGRectMake(CGRectGetMidX(childView.frame),
                                             CGRectGetMinY(childView.frame),
                                             tabBarButtonW,
                                             CGRectGetHeight(childView.frame));
                // 仅修改childView的x
                childView.frame = CGRectMake(buttonIndex*tabBarButtonW,
                                             CGRectGetMinY(childView.frame),
                                             CGRectGetWidth(childView.frame),
                                             CGRectGetHeight(childView.frame));
                buttonIndex++;
            }
        }
    }
    
}

/**
 *  Capturing touches on a subview outside the frame of its superview
 */
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (!self.clipsToBounds && !self.hidden && self.alpha > 0) {
        for (UIView *subView in self.subviews.reverseObjectEnumerator) {
            CGPoint subPoint = [subView convertPoint:point fromView:self];
            UIView *result = [subView hitTest:subPoint withEvent:event];
            if (result != nil) {
                return result;
            }
        }
    }
    return nil;
}
@end
